var ClubZhiDingUesrRecordItem = Item.extend({
    _ccsFileName: res.ClubZhiDingUesrRecordItem_json,
    _clubId: null,
    _recordData: null,
    initItem: function (clubId, data) {
        this._clubId = clubId;
        this._recordData = data;


        // this._nodeList['mark_icon1'].setVisible(false);
        // this._nodeList['mark_icon0'].setVisible(false);
        this._nodeList['mark_icon0'].mark = 1;
        this._nodeList['mark_icon1'].mark = 0;
    },

    refreshData: function (clubId, info) {
        this._clubId = clubId;
        this._recordData = info;
        cc.log('战绩数据:' + JSON.stringify(this._recordData));

        //如果当前可以显示标记 并且mard_record == 1时 显示已标记
        if (info['mard_record'] != undefined && info['mard_record'] == 1) {
            this._nodeList['mark_icon1'].setVisible(true);
        } else {
            this._nodeList['mark_icon1'].setVisible(false);
        }

        this.refreshHead(info);
        this._nodeList['wanFaName'].setString(ClubDataManager.getWanfaInfo2WanfaName(info));
        this._nodeList['roomId'].setString('房号:' + info['room_id']);
        this._nodeList['roomId'].width = 300;
        this._nodeList['createTime'].setString('创建时间' + timestamp2time(info['create_time']));
        //这里直接用的老代码
        if (info['wanfa']) {
            if (!_.isNumber(info['cur_round'])) {
                this._nodeList['round'].setString('局数:' + info['total_round']);
            } else {
                this._nodeList['round'].setString('局数:' + info['cur_round'] + '/' + info['total_round']);
            }
        } else {
            this._nodeList['round'].setString('局数:' + info['total_round']);
        }
    },


    _initClickListeners: function () {
        this._clickListeners = {
            'mark_icon0': '_clickMarkHandler',
            'mark_icon1': '_clickMarkHandler',
        }
    },

    //0未标记按钮  1已经标记按钮
    _clickMarkHandler: function (target) {
        this._nodeList['mark_icon1'].setVisible((target.mark == 1));
        var uri = '/MarkRecord/' + app.clubRecordArea + '?owner_uid=' + this._recordData.owner_uid + '&RoomId=' + this._recordData.room_id +
            '&CreateTime=' + this._recordData.create_time + '&markRecord=' + target.mark;
        var sign = Crypto.MD5('request:' + uri).toString();
        http.get("http://rec.yygameapi.com:56790" + uri + "&sign=" + sign, this._recordsRequestHandler.bind(this));
    },

    _recordsRequestHandler: function () {

    },

    //获取战绩玩家人数
    //TODO 这里是复用的旧代码 后续需要优化
    getMplayerNum: function (data) {
        var m_player_num = 0;
        //循环查找当前回放是几人玩
        for (var i = 0; i < 9; i++) {
            if (data['uid' + (i + 1)]) {
                m_player_num = i + 1;
            } else {
                break;
            }
        }
        return m_player_num;
    },
    //隐藏所有玩家头像
    hideAllHead: function () {
        //循环隐藏所有的头像
        for (var i = 1; i < 9; i++) {
            var headNode = this._nodeList['head' + i];
            if (headNode && cc.sys.isObjectValid(headNode)) {
                headNode.setVisible(false);
            }
        }
    },
    //显示所有头像
    viewHead: function (m_player_num) {
        for (var i = 0; i < m_player_num; i++) {
            var node = this._nodeList['head' + i];
            node.setVisible(true);
        }
    },
    //整理数据得到 最大的分数和玩家头像信息已经玩家对于的分数
    cardingData: function (data, m_player_num) {
        var maxScore = -100;
        var maxIndexArr = [];
        var userInfoArr = [];
        for (var i = 0; i < m_player_num; i++) {
            userInfoArr[i] = [];
            var head = null;
            var score = null;
            if (data['resultscore']) {
                score = data['resultscore'][i];
                if (score && score >= maxScore)
                    maxScore = data['resultscore'][i];
                maxIndexArr.push(i);
            }

            if (!data['nicknames']) {
                if (data['nickname' + (i + 1)]) {
                    userInfoArr[i]['nickname'] = data['nickname' + (i + 1)];
                }
                if (data['uid' + (i + 1)]) {
                    userInfoArr[i]['uid'] = data['uid' + (i + 1)];
                }
                userInfoArr[i]['score'] = data['resultscore'][i];
            }
            if (data['heads'].length > 0) {
                if (data['heads'][0] instanceof Array) {//字牌传的二维数组，麻将传的一维数组
                    head = data['heads'][0][i];
                } else {
                    if (_.isString(data['heads'])) {
                        head = data['heads'].split(',')[i];
                    } else {
                        head = data['heads'][i];
                    }
                }
                if (head) {
                    head = head.replace('\'', '');
                    head = head.replace('\'', '');
                }
                userInfoArr[i]['head'] = head;
            }
        }
        return {userInfoArr: userInfoArr, maxIndexArr: maxIndexArr, maxScore: maxScore};
    },

    //刷新头像显示
    refreshHead: function (data) {
        var playerNum = this.getMplayerNum(data);
        this.hideAllHead();
        this.viewHead(playerNum);

        var objData = this.cardingData(data, playerNum);
        var maxScore = objData.maxScore;
        var maxIndexArr = objData.maxIndexArr;
        var userInfoArr = objData.userInfoArr;


        for (var i = 0; i < playerNum; i++) {
            //显示信息
            // var info = this._$('info_' + (i + 1));
            var info = this._nodeList['head' + i];
            var headStr = 'head' + i + '.';
            var playerName = this._nodeList[headStr + 'playerName'];
            var playerId = this._nodeList[headStr + 'playerId'];
            var headImage = this._nodeList[headStr + 'head'];
            var playerScore = this._nodeList[headStr + 'playerScore'];

            var head = userInfoArr[i]['head'];
            var uid = userInfoArr[i]['uid'];
            var score = userInfoArr[i]['score'];

            var nickname = userInfoArr[i]['nickname'];

            if (nickname || uid) {
                playerName.setString(ellipsisStr(nickname, ClubDataManager.NirckNameSize));
                playerId.setString(ellipsisStr('ID ' + uid));
                if (head == undefined || head == "") {
                    head = res.defaultHead;
                }
                loadImageToSprite(head, headImage, true);
                if (score != undefined) {
                    if (score >= 0) {
                        score = '+' + score;
                    }
                    playerScore.setString(score);
                } else {
                    playerScore.setVisible(false);
                }
            } else {
                info.setVisible(false);
            }
        }
    }
});